package com.css.dzj.zfjg.modules.supervisioncompinfo.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.*;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgBlComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgDfComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgFbComplaintService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.service.JgTsjbAccessoryService;
import com.css.dzj.zfjg.modules.supervisioncompinfo.vo.TsXqVo;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 任务答复controller
 * @author  xgdou
 * @date  2020/05/25
 */
@RestController
@RequestMapping(value = "/lhjgtsrwdf/")
public class JgDfComplaintController {
    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    JgDfComplaintService jgDfComplaintService;
    @Autowired
    JgTsjbAccessoryService jgTsjbAccessoryService;
    @Autowired
    JgBlComplaintService jgBlComplaintService;
    @Autowired
    JgFbComplaintService jgFbComplaintService;
    @Autowired
    private ServiceUtil serviceUtil;



    /**
     * 调解信息保存
     */
    @GetMapping("savemedite")
    public R savemedite(@RequestBody JgTsjbMediateInfoEntity jgTsjbMediateInfoEntity, HttpServletRequest request) {
        try{
            JgTsjbMediateInfoEntity oldJgTsjbMediateInfoEntity = jgFbComplaintService.findMediate(jgTsjbMediateInfoEntity.getRegisterCode());
            if(oldJgTsjbMediateInfoEntity!=null) {
                BeanUtil.copyBeanNotEmptyToBean(jgTsjbMediateInfoEntity, oldJgTsjbMediateInfoEntity);
            }
            jgFbComplaintService.addmedite(jgTsjbMediateInfoEntity, request);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error("提交失败");
        }

    }
    /**
     * 答复内容保存
     * @param tsXqVo
     */
    @PostMapping("save")
    public R save(@RequestBody TsXqVo tsXqVo, HttpServletRequest request) {
        try{
            //保存答复信息
            JgTsjbHandleInfoEntity  jgTsjbHandleInfoEntity = tsXqVo.getJgTsjbHandleInfoEntity();
            R r = serviceUtil.getUserInfoByRequest(request);
            LoginUser userInfo=new LoginUser();
            if(r.getCode().equals("200")) {
                userInfo =  (LoginUser)r.get("data");
            }
            jgTsjbHandleInfoEntity.setRegisterCode(tsXqVo.getJgTsjbComplaintInfoEntity().getRegisterCode());
            jgTsjbHandleInfoEntity.setHandleDepartmentName(userInfo.getOrgName());
            jgDfComplaintService.add(jgTsjbHandleInfoEntity);
            //保存调解信息
            JgTsjbMediateInfoEntity jgTsjbMediateInfoEntity = tsXqVo.getJgTsjbMediateInfoEntity();
            jgDfComplaintService.addmediate(jgTsjbMediateInfoEntity);
            //保存附件信息
           // JgTsjbAccessoryEntity jgTsjbAccessoryEntity = tsXqVo.getJgTsjbAccessoryEntity();
            //jgTsjbAccessoryService.addfj(jgTsjbAccessoryEntity);
            //修改分办信息状态改为已答复103
            JgFbComplaintEntity jgFbComplaintEntity = tsXqVo.getJgFbComplaintEntity();
            String flag = "103";
            jgBlComplaintService.update(jgFbComplaintEntity,request,flag);
            //修改主表信息状态为已答复103
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity =  tsXqVo.getJgTsjbComplaintInfoEntity();
            jgBlComplaintService.updatetsjb(jgTsjbComplaintInfoEntity,request,flag);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error("提交失败");
        }

    }
    /**
     * 获取答复信息页面
     * @param registerCode
     * @return 监管投诉信息表  任务分办表  任务催办表
     */
    @GetMapping("getdfinfo")
    public R getdfinfo(@RequestParam String registerCode) {
        String s ="";
        try{
            //监管投诉信息
            JgTsjbComplaintInfoEntity jgTsjbComplaintInfoEntity = jgFbComplaintService.findNo(registerCode);
            //监管任务分办表
            JgFbComplaintEntity jgFbComplaintEntity = jgFbComplaintService.findRegister(registerCode);
            //任务催办表
            JgTsjbUrgeInfoEntity jgTsjbUrgeInfoEntity = jgFbComplaintService.findCode(registerCode);
            TsXqVo tsXqVo3 = new TsXqVo();
            tsXqVo3.setJgTsjbComplaintInfoEntity(jgTsjbComplaintInfoEntity);
            tsXqVo3.setJgFbComplaintEntity(jgFbComplaintEntity);
            tsXqVo3.setJgTsjbUrgeInfoEntity(jgTsjbUrgeInfoEntity);
            s = JSON.toJSONString(tsXqVo3);
            sysAuditService.saveSuccessAudit("查询信息成功", Constant.ZFJG_LHJGTSRWDF, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("tsXqVo3", tsXqVo3);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询信息失败", Constant.ZFJG_LHJGTSRWDF, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }
}
